function addNum (num1: number, num2: number): number {
  return num1 + num2
}

// let addNum2: (x: number, y: number) => number
// addNum2 = (x: number, y: number): number => x + y
// addNum2 = (x: number, y: number) => x + y

// type addNum = (x: number, y: number) => number
// let addFunc: addNum
// addFunc = (arg1: number, arg2: number) => arg1 + arg2

type AddFunction = (arg1: number, arg2:number, arg3?: number) => number
// ?: 可以选参数放在必选参数后面

let addFunction: AddFunction
addFunction = (x, y, z) => x + y + (z ? z : 0)
console.log(addFunction(4, 8, 4))


function handleData(x: string): string[]
function handleData(x: number): number[]

// 以上两个是函数重载， 函数重载只能用function来定义

// 函数实体
function handleData(x: any): any {
  if (typeof x === 'string') {
    return x.split('')
  } else {
    return x.toString().split('').map((item: any) => Number(item))
  }
}

console.log(handleData('abc'));
console.log(handleData(123));

